<?php
namespace app\component\controller;
use think\Controller;
use think\Db;
/**
 * Created by phpstrom.
 * User:wangyahe
 * Date: 2017/7/10
 * Time: 17:18
 */
class Upload extends Controller
{
	//单图上传
	/**
	 *
	 */
	public function uploader()
	{
//		echo MIG_PATH;die;
//		halt($_POST);
		//这里打印一下图片上传的表单的名字；是file,所以讲下面的image改为file
//		halt($_FILES);
		// 获取表单上传文件 例如上传了001.jpg
		$file = request()->file('file');
		// 移动到框架应用根目录/public/uploads/ 目录下
		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
//		halt($info);
		if($info){
			$data = [
				'name'       => input('post.name'),//post提交的表单的名字
				'filename'   => $info->getFilename(),//文件的名字
				'path'       => 'uploads/'.$info->getSaveName(),//?为什么是这个路径
				'extension'  => $info->getExtension(),//上传文件的扩展名
				'createtime' => time(),
				'size'       => $info->getSize(),//获取文件大小
			];
//			$data = array_merge( $data, Request::post(), [ ] );
			//将数据传到数据库中之后
			Db::table( 'hb_attachment' )->insert( $data );//这里为什么使用name
			//进行ajax返回
			echo json_encode([ 'valid' => 1, 'message' => 'uploads/'.$info->getSaveName()] );
		}else{
			// 上传失败获取错误信息
			echo json_encode([ 'valid' => 0, 'message' =>$file->getError() ]);
		}
	}
	public function filesLists(){
		$db = Db::name( 'attachment' )
			->whereIn( 'extension', explode( ',', strtolower( input('post.extensions') ) ) )
			->order( 'id desc');
		$Res  = $db->paginate( 2 );
		$data = [ ];
		if ( $Res->toArray() ) {

			foreach ( $Res as $k => $v ) {
				$data[ $k ]['createtime'] = date( 'Y/m/d', $v['createtime'] );
				$data[ $k ]['size']       = $v['size'];
				$data[ $k ]['url']        = IMG_PATH  .$v['path'];
				$data[ $k ]['path']       = $v['path'];
				$data[ $k ]['name']       = $v['name'];
			}
		}

		echo json_encode([ 'data' => $data, 'page' =>  is_null($Res->render()) ? '' : $Res->render()] );
	}






}